package com.capa.ui.memo

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import com.capa.BR
import com.capa.R
import com.capa.basic.BaseFragment
import com.capa.databinding.FragmentMemoBinding
import com.capa.ui.chat.adapter.ChatAdapter
import com.capa.ui.memo.adpter.MemoAdapter

class MemoFragment : BaseFragment<FragmentMemoBinding, MemoViewModel>() {

    private lateinit var adapter:MemoAdapter
    override fun initVariableId(): Int {
        return BR.model
    }

    override fun initContentView(
        inflater: LayoutInflater?,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): Int {
        return R.layout.fragment_memo
    }

    override fun initParam() {
        //

    }

    override fun initViewObservable() {
        adapter = MemoAdapter()
        binding?.rvNotes?.adapter = adapter

        viewModel?.memos?.observe(this, androidx.lifecycle.Observer {
            val oldSize = adapter.currentList.size
            adapter.submitList(it){
                if (oldSize == 0) {
                    binding?.rvNotes?.scrollToPosition(0)
                }
                if (it.size > oldSize) {
                    binding?.rvNotes?.scrollToPosition((it.size - 1).coerceAtLeast(0))
                }
            }
        })
    }

}